﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using SpringStudio.DAL;
using Sdepci.InfoCode.BLL.CodeIndex;

namespace Sdepci.InfoCode.Web.CodeIndex
{
    public partial class DictEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindData();
            }
        }

        private void BindData()
        {
            if (this.Request.QueryString["DictGuid"] != null && this.Request.QueryString["DictGuid"].Trim().Length > 0)
            {
                this.lblTitle.Text += "[修改]";

                Dict dict = new Dict(this.Request.QueryString["DictGuid"].Trim());
                this.DictCode.Text = dict.DictCode;
                this.DictText.Text = dict.DictText;
                this.DictDesc.Text = dict.DictDesc;
                this.Seq.Text = dict.Seq.ToString();
            }
            else if (this.Request.QueryString["CateGuid"] != null && this.Request.QueryString["CateGuid"].Trim().Length > 0 && this.Request.QueryString["ParentDictGuid"] != null && this.Request.QueryString["ParentDictGuid"].Trim().Length > 0)
            {
                this.lblTitle.Text += "[新增]";
            }
            else
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"参数错误！\");self.close();</script>");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string msg = string.Empty;
            if (this.Request.QueryString["DictGuid"] != null && this.Request.QueryString["DictGuid"].Trim().Length > 0)
            {
                bool result = new DictManager().Update(this.Request.QueryString["DictGuid"].Trim(), this.DictCode.Text.Trim(), this.DictText.Text.Trim(), this.DictDesc.Text.Trim(), int.Parse(this.Seq.Text.Trim()));
                msg = result ? "字典修改成功！" : "字典修改失败！";

            }
            else if (this.Request.QueryString["CateGuid"] != null && this.Request.QueryString["CateGuid"].Trim().Length > 0 && this.Request.QueryString["ParentDictGuid"] != null && this.Request.QueryString["ParentDictGuid"].Trim().Length > 0)
            {
                bool result = new DictManager().Add(this.Request.QueryString["ParentDictGuid"].Trim(), this.Request.QueryString["CateGuid"].Trim(), this.DictCode.Text.Trim(), this.DictText.Text.Trim(), this.DictDesc.Text.Trim(), int.Parse(this.Seq.Text.Trim()));
                msg = result ? "字典新增成功！" : "字典新增失败！";
            }
            else
            {
                msg = "参数错误！";
            }

            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");self.returnValue=true;self.close();</script>");
        }
    }
}
